package com.dai.zhenfujin.visitingcard.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.Constants.EventConstants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.adapter.FansImagesAdpter;
import com.dai.zhenfujin.bean.AlumFansListBean;
import com.dai.zhenfujin.bean.MessageEvent;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.SPUtils;
import com.dai.zhenfujin.view.CommonDialog;
import com.dai.zhenfujin.view.SodukuGridView;
import com.dai.zhenfujin.visitingcard.activity.CommentDetailsActivity;
import com.dai.zhenfujin.visitingcard.app.PersonalDetailsActivity;
import com.dai.zhenfujin.visitingcard.bean.Image;
import com.dai.zhenfujin.visitingcard.view.ScreenTools;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.onekeyshare.OnekeyShare;
import de.hdodenhof.circleimageview.CircleImageView;

public class FansAdapter extends BaseAdapter {
    private Context context;
    private List<AlumFansListBean.MsgListBean> datalist;
    private int type2;
    private List<Boolean> alreadyLikeList = new ArrayList<>();

    public FansAdapter(Context context, List<AlumFansListBean.MsgListBean> datalist) {
        this.context = context;
        this.datalist = datalist;
    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fans_listvew, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.grid_view = (SodukuGridView) convertView.findViewById(R.id.grid_view);
          //  viewHolder.ivOne = (CustomImageView) convertView.findViewById(R.id.iv_oneimage);
            viewHolder.item_time = (TextView) convertView.findViewById(R.id.item_time);
            viewHolder.tv_connect = (TextView) convertView.findViewById(R.id.tv_connect);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);
            viewHolder.ll_lick = (LinearLayout) convertView.findViewById(R.id.ll_lick);
            viewHolder.ll_comment =  convertView.findViewById(R.id.ll_comment);
            viewHolder.ll_fans =  convertView.findViewById(R.id.ll_fans);
            viewHolder.iv_lick =  convertView.findViewById(R.id.iv_lick);
            viewHolder.ll_share =  convertView.findViewById(R.id.ll_share);
            viewHolder.bt_clear =  convertView.findViewById(R.id.bt_clear);
            viewHolder.tv_comment_num =  convertView.findViewById(R.id.tv_comment_num);
            viewHolder.tv_like_num =  convertView.findViewById(R.id.tv_like_num);
            viewHolder.pp_adapter_position =  convertView.findViewById(R.id.pp_adapter_position);//职业
            viewHolder.pp_adapter_bloc =  convertView.findViewById(R.id.pp_adapter_bloc);//公司
            viewHolder.item_word_type =  convertView.findViewById(R.id.item_word_type);//主业
            viewHolder.cv_img = (CircleImageView) convertView.findViewById(R.id.cv_img);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        AlumFansListBean.MsgListBean itemList = datalist.get(position);
        viewHolder.tv_name.setText(itemList.getUser_name());
        viewHolder.item_time.setText(itemList.getCreate_time().substring(0,itemList.getCreate_time().length()-2));
        viewHolder.tv_connect.setText(itemList.getMsg_content());
        Glide.with(context).load(itemList.getUser_logo()).into(viewHolder.cv_img);
        viewHolder.pp_adapter_position.setText(itemList.getUser_duties());
        viewHolder.pp_adapter_bloc.setText("公司:" + itemList.getUser_company());
        viewHolder.item_word_type.setText("主业:" + itemList.user_job);
        viewHolder.tv_comment_num.setText(itemList.getCom_num());
        viewHolder.tv_like_num.setText(itemList.getLike_num());


        if (itemList.getUrl_list().size() > 0) {
            List<String> list = itemList.getUrl_list();
            FansImagesAdpter fansImagesAdpter = new FansImagesAdpter(context,list);
            viewHolder.grid_view.setAdapter(fansImagesAdpter);
           // viewHolder.grid_view.setIsShowAll(false); //传入的图片数超过9张时，是否全部显示
           // viewHolder.grid_view.setUrlList(itemList.getUrl_list());
        }

        viewHolder.cv_img.setOnClickListener(v -> {
            if (!itemList.getUser_id().equals(SPUtils.getString(context,Constants.USER_MOBILE))) {
                //头像
                SPUtils.saveConversionHeadPhoto(context, itemList.getUser_id(), itemList.getUser_logo());
                //昵称
                SPUtils.saveConversionNickName(context, itemList.getUser_id(), itemList.getUser_name());
                //职业
                SPUtils.saveConversionUserDuties(context, itemList.getUser_id(), itemList.getUser_duties());

                Intent intent = new Intent(context, PersonalDetailsActivity.class);
                intent.putExtra("user_id", itemList.getUser_id());
                context.startActivity(intent);
            }
        });

        viewHolder.ll_fans.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("itemList",itemList);
            intent.setClass(context,CommentDetailsActivity.class);
            context.startActivity(intent);
        });

        if (SPUtils.getBoolean(context,"flag")){
            viewHolder.bt_clear.setVisibility(View.VISIBLE);
            viewHolder.bt_clear.setOnClickListener(v -> {
                new CommonDialog(context, R.style.dialog, false, "确认删除吗", (dialog, confirm) -> {
                    if (confirm) {
                        Map<String, Object> params = new HashMap<>();
                        Map<String, String> params2 = new HashMap<>();
                        params2.put("msg_id", itemList.getMsg_id());
                        params.put("req_bd",  params2);
                        HttpClient.post(context, Constants.DELMSG, params, new CallBack<AlumFansListBean>() {
                            @Override
                            public void onSuccess(AlumFansListBean resultBean) {
                                datalist.remove(position);
                                notifyDataSetInvalidated();
                             //   notifyDataSetChanged();
                                MessageEvent messageEvent = new MessageEvent();
                                messageEvent.setMessageKey(EventConstants.UPDATA_FANS_LIST);
                                EventBus.getDefault().post(messageEvent);
                            }
                        });
                    }
                    dialog.dismiss();
                }).show();
            });
        }else {
            viewHolder.bt_clear.setVisibility(View.INVISIBLE);
        }

        viewHolder.ll_share.setOnClickListener(v -> {
            //一建分享
            OnekeyShare oks = new OnekeyShare();
            //关闭sso授权
            oks.disableSSOWhenAuthorize();


            // title标题，微信、QQ和QQ空间等平台使用
            oks.setTitle("分享");
            // titleUrl QQ和QQ空间跳转链接
          //  oks.setTitleUrl("http://sharesdk.cn");
            // text是分享文本，所有平台都需要这个字段
            oks.setText("我是分享文本");
            // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
            oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片

            // url在微信、微博，Facebook等平台中使用
           // oks.setUrl("http://sharesdk.cn");
            oks.setUrl("https://www.zhenfujin.net/public/share.php?proid=201908151422446895328000000012&code=");
            // comment是我对这条分享的评论，仅在人人网使用
            oks.setComment("我是测试评论文本");
            // 启动分享GUI
            oks.show(context);
        });

        alreadyLikeList.add(itemList.getIs_like().equals("1"));
        if (alreadyLikeList.get(position)){
            viewHolder.iv_lick.setImageResource(R.mipmap.icon_dianzan2);
            // alreadyLikeList.add(itemList.getIs_like().equals("1")); //0 未点赞
        }else {
            viewHolder.iv_lick.setImageResource(R.mipmap.icon_dianzan);
            //  alreadyLikeList.add(itemList.getIs_like().equals("0")); //已点赞
        }
        viewHolder.ll_lick.setOnClickListener(v -> {
            Map<String, Object> params = new HashMap<>();
            Map<String, String> params2 = new HashMap<>();
            params2.put("msg_id", itemList.getMsg_id());

            //点赞
            if (alreadyLikeList.get(position)){
                params2.put("type",2 + ""); //取消点赞
            }else {
                params2.put("type",1 + ""); //点赞
              //  alreadyLikeList.add(itemList.getIs_like().equals("0")); //0 未点赞
            }

            params.put("req_bd",  params2);
            HttpClient.post(context, Constants.LIKE, params, new CallBack<AlumFansListBean>() {
                @Override
                public void onSuccess(AlumFansListBean resultBean) {
                   // viewHolder.iv_lick.setBackground(context.getResources().getDrawable(R.mipmap.icon_dianzan2));
                    if (alreadyLikeList.get(position)){
                        viewHolder.iv_lick.setImageResource(R.mipmap.icon_dianzan);
                        viewHolder.tv_like_num.setText(Integer.valueOf(viewHolder.tv_like_num.getText().toString()) - 1 + "");
                        alreadyLikeList.set(position,false);
                    }else{
                        viewHolder.iv_lick.setImageResource(R.mipmap.icon_dianzan2);
                        viewHolder.tv_like_num.setText(Integer.valueOf(viewHolder.tv_like_num.getText().toString()) + 1 + "");
                        alreadyLikeList.set(position,true);
                    }
                }
            });
        });

        /*SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        viewHolder.item_time.setText(str);*/
        return convertView;
    }

    private void handlerOneImage(ViewHolder viewHolder, Image image) {
        int totalWidth;
        int imageWidth;
        int imageHeight;
        ScreenTools screentools = ScreenTools.instance(context);
        totalWidth = screentools.getScreenWidth() - screentools.dip2px(80);
        imageWidth = screentools.dip2px(image.getWidth());
        imageHeight = screentools.dip2px(image.getHeight());
        if (image.getWidth() <= image.getHeight()) {
            if (imageHeight > totalWidth) {
                imageHeight = totalWidth;
                imageWidth = (imageHeight * image.getWidth()) / image.getHeight();
            }
        } else {
            if (imageWidth > totalWidth) {
                imageWidth = totalWidth;
                imageHeight = (imageWidth * image.getHeight()) / image.getWidth();
            }
        }
      /*  ViewGroup.LayoutParams layoutparams = viewHolder.ivOne.getLayoutParams();
        layoutparams.height = imageHeight;
        layoutparams.width = imageWidth;
        viewHolder.ivOne.setLayoutParams(layoutparams);
        viewHolder.ivOne.setClickable(true);
        viewHolder.ivOne.setScaleType(android.widget.ImageView.ScaleType.FIT_XY);
        viewHolder.ivOne.setImageUrl(image.getUrl());*/
    }


    class ViewHolder {
        public SodukuGridView grid_view;
       // public CustomImageView ivOne;
        public CircleImageView cv_img;
        public ImageView iv_lick;
        public Button bt_clear;
        public TextView item_time,tv_name,tv_connect,tv_comment,tv_comment_num,tv_like_num;
        public TextView tvcomment,tvpraise,pp_adapter_position,pp_adapter_bloc,item_word_type;
        public LinearLayout ll_comment,ll_fans,ll_lick,ll_share;
    }
}
